//
//  GTVideoViewController.m
//  news
//
//  Copyright © 2022 sinosun. All rights reserved.
//

#import "GTVideoViewController.h"
#import "GTVideoCoverViewCell.h"

@interface GTVideoViewController () <UICollectionViewDelegate, UICollectionViewDataSource>

@end

@implementation GTVideoViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        self.tabBarItem.title = @"视频";
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor orangeColor];
    
    // 1. 先构造布局，流式布局
    UICollectionViewFlowLayout *collectionViewFlowLayout = [[UICollectionViewFlowLayout alloc] init];
    collectionViewFlowLayout.minimumLineSpacing = 10;
    collectionViewFlowLayout.minimumInteritemSpacing = 10;
    collectionViewFlowLayout.itemSize = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.width/ 16 * 9);
    // 2. 初始化 UICollectionView
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:collectionViewFlowLayout];
    // 3. 给 UICollectionView 注册 UICollectionViewCell
    [collectionView registerClass:[GTVideoCoverViewCell class] forCellWithReuseIdentifier:@"id"];
    // 4. 给 UICollectionView 设置 dataSource 及 delegate
    collectionView.dataSource = self;
    collectionView.delegate = self;
    [self.view addSubview:collectionView];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 20;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    // 先去系统的回收池里面取一个cell
    
    if (false) {
        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"id" forIndexPath:indexPath];
        cell.backgroundColor = [UIColor orangeColor];
    }
    
    // 使用自定义的cell
    GTVideoCoverViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"id" forIndexPath:indexPath];
    
    if ([cell isKindOfClass:[GTVideoCoverViewCell class]]) {
        cell.backgroundColor = [UIColor orangeColor];
        [cell layoutWithVideoCoverUrl:@"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpng.pngtree.com%2Fthumb_back%2Ffw800%2Fbackground%2F20190222%2Fourmid%2Fpngtree-happy-note-advertising-background-spotbeautifulroundadvertising-backgroundleaves-image_63561.jpg&refer=http%3A%2F%2Fpng.pngtree.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1669127438&t=19d47b3d8ff36165d3f5386c4ee9433d" videoUrl:@"https://vd3.bdstatic.com/mda-ngq507t8y1d08dtf/sc/cae_h264/1658738994994619370/mda-ngq507t8y1d08dtf.mp4?v_from_s=hkapp-haokan-suzhou&auth_key=1666590863-0-0-9a24220952d3c773666987c5d36133a7&bcevod_channel=searchbox_feed&cd=0&pd=1&pt=3&logid=1463493711&vid=18074281592631778094&abtest=104959_1-104859_1&klogid=1463493711"];
    }
    
    
    
    
    
    return cell;
}

#pragma mark - UICollectionViewDelegateFlowLayout 布局的代理

// 根据 indexPath 区分地处理某个 itemSize
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
//    
//    if (indexPath.item % 3 == 0) {
//        return CGSizeMake(self.view.frame.size.width, 100);
//    }
//    return CGSizeMake((self.view.frame.size.width-10)/2, 300);
//
//}
@end
